package com.google.code.sysarch.v3.pre;

import java.io.*;
import java.util.Date;
import java.text.DateFormat;

/**
 * This class bundles some static methods making life easier
 * @author Fabian Nicolay
 */
public class Util {
	
	/** The message type for errors*/
	public static final int MTYPE_ERROR=0;
	/** The message type for info messages*/
	public static final int MTYPE_INFO=1;
	/** The message type for debug messages*/
	public static final int MTYPE_DEBUG=2;
	
	//Change the next 2 vars to set up which kind of messages should be printed
	private static int consoleLevel = MTYPE_DEBUG;
	private static int logLevel = MTYPE_INFO;
	
	private static PrintWriter logFile;
	
	/**
	 * Prints an message to the console and/or a logfile
	 * @param message the error message
	 * @param type the type of the message, MTYPE_ERROR, MTYPE_INFO or MTYPE_DEBUG
	 */
	public static void printMessage(String message, int type) {
		
		String typeString = "Unknown";
		
		switch (type) {
			case MTYPE_ERROR: typeString = "[Error]"; break;
			case MTYPE_INFO: typeString = "[Info]"; break;
			case MTYPE_DEBUG: typeString = "[Debug]"; break;	
		} //end switch(type)
		
		String outMessage = DateFormat.getInstance().format(new Date()) 
									+ "\t" + typeString + "\t" + message;
		
		
		if (type <= consoleLevel) {
			System.out.println(outMessage);
		} 
		
		if (type <= logLevel) {
			if (logFile == null) {
				try {
					logFile = new PrintWriter("glhazard.log");
					Util.printMessage("Logfile created", MTYPE_INFO);
				} catch (FileNotFoundException e) {
					Util.printMessage("Unable to write into \"glhazard.log\"",
							MTYPE_ERROR);
				} 
			}
			
			logFile.println(outMessage);
			logFile.flush();
		}
		
		
	} 

	/** 
	 * Adds to float arrays
	 * @param array1 the first array
	 * @param array2 the second array
	 * @return the total of the two arrays
	 */
	public static float[] addArrays(float[] array1, float[] array2) {
		if (array1.length != array2.length) {
			printMessage("Wrong Arraysize!", MTYPE_ERROR );
			return null;
		} else {
			for (int i = 0; i < array1.length; i++ ) {
				array1[i] += array2[i];
			}
 		}
		return array1;	
	}
	
	/**
	 * Adds all elements of the array together
	 * @param array the array
	 * @return the total sum of all elements
	 */
	public static float sumArray(float[] array) {
		float total = 0;
		for (int i=0; i < array.length; i++) {
			total += array[i];
		}
		return total;		
	}
	
	
	/**
	 * The consoleInput() Method allows to read the console-input, 
	 * similiar to C/C++'s cin fuction;
	 * @return
	 */
	public static String consoleInput(){
		String result = "";

		BufferedInputStream iStream = new  BufferedInputStream(System.in);
		
		char character;
		try {
			while ((character = (char) iStream.read()) != '\n') {
				result = result + character;
			} 
		} catch (IOException e) {
			return null;
		} 
		result = result.substring(0, result.length()-1);
		return result;
	}	
	
	/** Just for testing the util methods */
	public static void main(String args[]) {
		System.out.print("Eingabe: "); String result = consoleInput();
		System.out.println("Ausgabe: " + result);
		
		printMessage("Testfehler", MTYPE_ERROR);
		printMessage("Eingabe: " + result, MTYPE_INFO);
		printMessage("Systemzeit: " + System.currentTimeMillis(), MTYPE_DEBUG);
	} 

	public static int getNumberOfLines(String filename) {
		int numLines = 0;
		try {
			BufferedReader reader = new BufferedReader(new FileReader(filename));
			while (reader.readLine() != null) {
				numLines++;
			}	
			reader.close();
		} catch (FileNotFoundException e) {
			Util.printMessage("File '"+filename+"' not found.",Util.MTYPE_ERROR);
		} 
		catch (IOException e) {
			Util.printMessage("Error while reading file '"+filename+"'.",Util.MTYPE_ERROR);
		}
		
		
		
		return numLines;
	}

}
